Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 13 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { basename, extname, relative } = require('path');
const { getOptions } = require('loader-utils');
const VirtualModules = require('./lib/virtual');
const posixify = require('./lib/posixify');

const {
Expand Down Expand Up @@ -68,20 +67,22 @@ function deprecatePreprocessOptions(options) {
options.preprocess = options.preprocess || preprocessOptions;
}

const virtualModuleInstances = new Map();
const virtualModules = new Map();
let index = 0;

module.exports = function(source, map) {
if (this._compiler && !virtualModuleInstances.has(this._compiler)) {
virtualModuleInstances.set(this._compiler, new VirtualModules(this._compiler));
}

const virtualModules = virtualModuleInstances.get(this._compiler);

this.cacheable();

const options = Object.assign({}, getOptions(this));
const callback = this.async();

if (options.cssPath) {
const css = virtualModules.get(options.cssPath);
virtualModules.delete(options.cssPath);
callback(null, css);
return;
}

const isServer = this.target === 'node' || (options.generate && options.generate == 'ssr');
const isProduction = this.minimize || process.env.NODE_ENV === 'production';

Expand Down Expand Up @@ -134,17 +135,11 @@ module.exports = function(source, map) {
}

if (options.emitCss && css.code) {
const cssFilepath = compileOptions.filename.replace(
/\.[^/.]+$/,
`.svelte.css`
);

const resource = posixify(compileOptions.filename);
const cssPath = `${resource}.${index++}.css`;
css.code += '\n/*# sourceMappingURL=' + css.map.toUrl() + '*/';
js.code = js.code + `\nimport '${posixify(cssFilepath)}';\n`;

if (virtualModules) {
virtualModules.writeModule(cssFilepath, css.code);
}
js.code += `\nimport '${cssPath}!=!svelte-loader-hot?cssPath=${cssPath}!${resource}'\n;`;
virtualModules.set(cssPath, css.code);
}

callback(null, js.code, js.map);
Expand Down
89 changes: 0 additions & 89 deletions lib/virtual-stats.js

This file was deleted.

81 changes: 0 additions & 81 deletions lib/virtual.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ describe('loader', () => {
function(err, code, map) {
expect(err).not.to.exist;

expect(code).to.match(/import '.+\.css';/);
expect(code).to.match(/!=!svelte-loader-hot\?cssPath=/);
},
{ emitCss: true }
)
Expand Down