Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Implement bundling files from @import statements. #139

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
33 changes: 30 additions & 3 deletions css-plugin-base-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ exports.bundle = function(loads, compileOpts, outputOpts) {
return;

var loader = this;
var importLoader;

// backwards compat with fileURL for rootURL
if (loader.rootURL && loader.rootURL.substr(0, 5) == 'file:')
Expand All @@ -70,6 +71,7 @@ exports.bundle = function(loads, compileOpts, outputOpts) {

var inputFiles = {};
cssLoads.forEach(function(load) {
importLoader = load.metadata.loader;
inputFiles[path.relative(baseURLPath, fromFileURL(load.address))] = {
source: load.metadata.style,
sourceMap: load.metadata.styleSourceMap
Expand All @@ -83,12 +85,34 @@ exports.bundle = function(loads, compileOpts, outputOpts) {
}

var cwd = process.cwd();
var oldTranslate = importLoader.translate;
var oldImport = importLoader.import;

importLoader.translate = function(load) {
return loader.translate.call(this, load).then(function() {
if(load.metadata.style) {
inputFiles[path.relative(baseURLPath, fromFileURL(load.address))] = {
source: load.metadata.style,
sourceMap: load.metadata.styleSourceMap
};
load.metadata.format = 'amd';
}
});
};

importLoader.import = loader.import;

var postCssPlugins = [atImport({
resolve: function(fileName, dirname, opts) {
if (absUrl(fileName))
return fileName;
return path.relative(baseURLPath, path.join(dirname, fileName));
var resolved = fileName;
if (!absUrl(fileName)) {
fileName = path.join(dirname, fileName);
resolved = path.relative(baseURLPath, fileName);
}

return importLoader.import(fileName, module.id).then(function() {
return resolved;
});
},
load: function(fileName, opts) {
if (absUrl(fileName))
Expand Down Expand Up @@ -158,6 +182,9 @@ exports.bundle = function(loads, compileOpts, outputOpts) {
.then(function(result) {
var cssOutput = result.css;

importLoader.translate = oldTranslate;
importLoader.import = oldImport;

// write a separate CSS file if necessary
if (loader.separateCSS) {
var outFile = path.resolve(outputOpts.outFile).replace(/\.js$/, '.css');
Expand Down
1 change: 1 addition & 0 deletions css-plugin-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function CSSPluginBase(compileCSS) {

return Promise.resolve(compileCSS.call(loader, load.source, load.address, load.metadata.loaderOptions || {}))
.then(function(result) {
Object.defineProperty(load.metadata, 'loader', { enumerable: false, value: loader });
load.metadata.style = result.css;
load.metadata.styleSourceMap = result.map;
if (result.moduleFormat)
Expand Down