Skip to content

Commit 90378cb

Browse files
committed
tidy up
1 parent a3d5e64 commit 90378cb

File tree

2 files changed

+75
-112
lines changed

2 files changed

+75
-112
lines changed

index.js

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
const { basename, extname, relative } = require('path');
22
const { compile, preprocess } = require('svelte');
33
const { getOptions } = require('loader-utils');
4-
const VirtualModulesPlugin = require('./lib/virtual');
4+
const VirtualModules = require('./lib/virtual');
55

66
const hotApi = require.resolve('./lib/hot-api.js');
7-
const virtualModules = new VirtualModulesPlugin();
87

98
function makeHot(id, code, hotOptions) {
109
const options = JSON.stringify(hotOptions);
1110
const replacement = `
12-
1311
if (module.hot) {
14-
1512
const { configure, register, reload } = require('${posixify(hotApi)}');
1613
1714
module.hot.accept();
@@ -26,7 +23,6 @@ if (module.hot) {
2623
}
2724
}
2825
29-
3026
export default $2;
3127
`;
3228

@@ -80,14 +76,15 @@ function deprecatePreprocessOptions(options) {
8076
options.preprocess = options.preprocess || preprocessOptions;
8177
}
8278

83-
const compilers = new Set();
79+
const virtualModuleInstances = new Map();
8480

8581
module.exports = function(source, map) {
86-
if (!compilers.has(this._compiler)) {
87-
virtualModules.apply(this._compiler);
88-
compilers.add(this._compiler);
82+
if (!virtualModuleInstances.has(this._compiler)) {
83+
virtualModuleInstances.set(this._compiler, new VirtualModules(this._compiler));
8984
}
9085

86+
const virtualModules = virtualModuleInstances.get(this._compiler);
87+
9188
this.cacheable();
9289

9390
const options = Object.assign({}, this.options, getOptions(this));
@@ -97,52 +94,39 @@ module.exports = function(source, map) {
9794
const isProduction = this.minimize || process.env.NODE_ENV === 'production';
9895

9996
options.filename = this.resourcePath;
100-
if (!options.format) {
101-
options.format = this.version === 1 ? options.format || 'cjs' : 'es';
102-
}
103-
if (!options.shared) {
104-
options.shared = options.format === 'es' && 'svelte/shared.js';
105-
}
106-
107-
if (options.emitCss) options.css = false;
108-
97+
if (!options.format) options.format = 'es';
98+
if (!options.shared) options.shared = options.format === 'es' && 'svelte/shared.js';
10999
if (!options.name) options.name = capitalize(sanitize(options.filename));
110-
111100
if (!options.onwarn) options.onwarn = warning => this.emitWarning(new Error(warning));
101+
if (options.emitCss) options.css = false;
112102

113103
deprecatePreprocessOptions(options);
114104
options.preprocess.filename = options.filename;
115105

116106
preprocess(source, options.preprocess).then(processed => {
117107
let { js, css } = normalize(compile(processed.toString(), options));
118108

109+
if (options.hotReload && !isProduction && !isServer) {
110+
const hotOptions = Object.assign({}, options.hotOptions);
111+
const id = JSON.stringify(relative(process.cwd(), options.filename));
112+
js.code = makeHot(id, js.code, hotOptions);
113+
}
114+
119115
if (options.emitCss && css.code) {
120116
const cssFilepath = options.filename.replace(
121117
/\.[^/.]+$/,
122118
`.svelte.css`
123119
);
124120
css.code += '\n/*# sourceMappingURL=' + css.map.toUrl() + '*/';
125-
js.code = js.code + `\nrequire('${cssFilepath}');\n`;
126-
/** If the webpack compiler is initialized, write the file to its vitual file system */
127-
if (this.fs) {
128-
virtualModules.writeModule(cssFilepath, css.code);
129-
}
130-
}
121+
js.code = js.code + `\nimport '${cssFilepath}';\n`;
131122

132-
if (options.hotReload && !isProduction && !isServer) {
133-
const hotOptions = Object.assign({}, options.hotOptions);
134-
const id = JSON.stringify(relative(process.cwd(), options.filename));
135-
js.code = makeHot(id, js.code, hotOptions);
123+
virtualModules.writeModule(cssFilepath, css.code);
136124
}
137125

138126
callback(null, js.code, js.map);
139127
}, err => callback(err)).catch(err => {
140-
console.log(err.stack);
141128
// wrap error to provide correct
142129
// context when logging to console
143130
callback(new Error(`${err.name}: ${err.toString()}`));
144131
});
145-
};
146-
147-
module.exports.plugin = virtualModules;
148-
module.exports.loader = __filename;
132+
};

lib/virtual.js

Lines changed: 57 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,74 @@
11
var VirtualStats = require('./virtual-stats');
22
var path = require('path');
3-
// var debug = require('debug')('webpack-virtual-modules');
43

54
var inode = 45000000;
65

7-
function VirtualModulesPlugin() {}
6+
// Adapted from https://github.com/sysgears/webpack-virtual-modules
7+
// MIT Licensed https://github.com/sysgears/webpack-virtual-modules/blob/master/LICENSE
88

9-
VirtualModulesPlugin.prototype.writeModule = function(filePath, contents) {
10-
var len = contents ? contents.length : 0;
11-
var time = Date.now();
9+
function VirtualModulesPlugin(compiler) {
10+
this.compiler = compiler;
1211

13-
var stats = new VirtualStats({
14-
dev: 8675309,
15-
nlink: 0,
16-
uid: 1000,
17-
gid: 1000,
18-
rdev: 0,
19-
blksize: 4096,
20-
ino: inode++,
21-
mode: 33188,
22-
size: len,
23-
blocks: Math.floor(len / 4096),
24-
atime: time,
25-
mtime: time,
26-
ctime: time,
27-
birthtime: time
28-
});
12+
if (!compiler.inputFileSystem._writeVirtualFile) {
13+
var originalPurge = compiler.inputFileSystem.purge;
2914

30-
// debug(this._compiler.name, "Write module:", modulePath, contents);
15+
compiler.inputFileSystem.purge = function() {
16+
originalPurge.call(this, arguments);
17+
if (this._virtualFiles) {
18+
Object.keys(this._virtualFiles).forEach(
19+
function(file) {
20+
var data = this._virtualFiles[file];
21+
setData(this._statStorage, file, [null, data.stats]);
22+
setData(this._readFileStorage, file, [null, data.contents]);
23+
}.bind(this)
24+
);
25+
}
26+
};
3127

32-
this._compiler.inputFileSystem._writeVirtualFile(filePath, stats, contents);
33-
if (this._watcher && this._watcher.watchFileSystem.watcher.fileWatchers.length) {
34-
this._watcher.watchFileSystem.watcher.fileWatchers.forEach((fileWatcher) => {
35-
if (fileWatcher.path === filePath) {
36-
// debug(this._compiler.name, "Emit file change:", filePath, time);
37-
fileWatcher.emit("change", time, null);
38-
}
39-
});
40-
}
41-
};
28+
compiler.inputFileSystem._writeVirtualFile = function(file, stats, contents) {
29+
this._virtualFiles = this._virtualFiles || {};
30+
this._virtualFiles[file] = { stats: stats, contents: contents };
31+
setData(this._statStorage, file, [null, stats]);
32+
setData(this._readFileStorage, file, [null, contents]);
33+
};
34+
}
4235

43-
function setData(storage, key, value) {
44-
if (storage.data instanceof Map) {
45-
storage.data.set(key, value);
46-
} else {
47-
storage.data[key] = value;
48-
}
36+
compiler.hooks.watchRun.tapAsync('VirtualModulesPlugin', (watcher, callback) => {
37+
this._watcher = watcher.compiler || watcher;
38+
callback();
39+
});
4940
}
5041

51-
VirtualModulesPlugin.prototype.apply = function(compiler) {
52-
this._compiler = compiler;
53-
54-
// var afterEnvironmentHook = function() {
55-
if (!compiler.inputFileSystem._writeVirtualFile) {
56-
var originalPurge = compiler.inputFileSystem.purge;
57-
58-
compiler.inputFileSystem.purge = function() {
59-
originalPurge.call(this, arguments);
60-
if (this._virtualFiles) {
61-
Object.keys(this._virtualFiles).forEach(function(file) {
62-
var data = this._virtualFiles[file];
63-
setData(this._statStorage, file, [null, data.stats]);
64-
setData(this._readFileStorage, file, [null, data.contents]);
65-
}.bind(this));
66-
}
67-
};
68-
69-
compiler.inputFileSystem._writeVirtualFile = function(file, stats, contents) {
70-
this._virtualFiles = this._virtualFiles || {};
71-
this._virtualFiles[file] = {stats: stats, contents: contents};
72-
setData(this._statStorage, file, [null, stats]);
73-
setData(this._readFileStorage, file, [null, contents]);
74-
};
75-
}
76-
// }
42+
VirtualModulesPlugin.prototype.writeModule = function(filePath, contents) {
43+
var len = contents ? contents.length : 0;
44+
var time = Date.now();
7745

78-
const watchRunHook = (watcher, callback) => {
79-
this._watcher = watcher.compiler || watcher;
80-
callback();
81-
}
46+
var stats = new VirtualStats({
47+
dev: 8675309,
48+
nlink: 0,
49+
uid: 1000,
50+
gid: 1000,
51+
rdev: 0,
52+
blksize: 4096,
53+
ino: inode++,
54+
mode: 33188,
55+
size: len,
56+
blocks: Math.floor(len / 4096),
57+
atime: time,
58+
mtime: time,
59+
ctime: time,
60+
birthtime: time
61+
});
8262

83-
if(compiler.hooks) {
84-
console.log('>>>1')
85-
// compiler.hooks.afterEnvironment.tap('VirtualModulesPlugin', afterEnvironmentHook);
86-
// compiler.hooks.afterResolvers.tap('VirtualModulesPlugin', afterResolversHook);
87-
compiler.hooks.watchRun.tapAsync('VirtualModulesPlugin', watchRunHook);
88-
} else {
89-
// compiler.plugin("after-environment", afterEnvironmentHook);
90-
// compiler.plugin("after-resolvers", afterResolversHook);
91-
// compiler.plugin("watch-run", watchRunHook);
92-
}
63+
this.compiler.inputFileSystem._writeVirtualFile(filePath, stats, contents);
9364
};
9465

66+
function setData(storage, key, value) {
67+
if (storage.data instanceof Map) {
68+
storage.data.set(key, value);
69+
} else {
70+
storage.data[key] = value;
71+
}
72+
}
73+
9574
module.exports = VirtualModulesPlugin;

0 commit comments

Comments
 (0)