Skip to content

Commit

Permalink
feat: loader schema
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkal committed Aug 18, 2019
1 parent 6072e66 commit c9627c6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/__tests__/__snapshots__/transform.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`doesn't rewrite an absolute path in url() declarations 1`] = `
"
.tpyglzj {
.title_tpyglzj {
background-image: url(/assets/test.jpg);
}
"
Expand Down Expand Up @@ -31,7 +31,7 @@ exports[`injects global rules to the global scope 1`] = `
exports[`rewrites a relative path in url() declarations 1`] = `
"
.tpyglzj {
.title_tpyglzj {
background-image: url(../linaria/assets/test.jpg);
}
"
Expand All @@ -40,7 +40,7 @@ exports[`rewrites a relative path in url() declarations 1`] = `
exports[`rewrites multiple relative paths in url() declarations 1`] = `
"
.tpyglzj {
.title_tpyglzj {
@font-face {
font-family: Test;
src: url(../linaria/assets/font.woff2) format(\\"woff2\\"), url(../linaria/assets/font.woff) format(\\"woff\\");
Expand Down
44 changes: 38 additions & 6 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,57 @@ import path from 'path';
import mkdirp from 'mkdirp';
import normalize from 'normalize-path';
import loaderUtils from 'loader-utils';
import validateOptions from 'schema-utils';
import enhancedResolve from 'enhanced-resolve';
import Module from './babel/module';
import transform from './utils/transform';

const schema = {
type: 'object',
properties: {
displayName: {
type: 'boolean',
},
evaluate: {
type: 'boolean',
},
prefix: {
type: 'string',
},
optimize: {
type: 'boolean',
},
sourceMap: {
type: 'boolean',
},
cacheDirectory: {
type: 'string',
},
},
};

export default function loader(
this: any,
content: string,
inputSourceMap: Object | null
) {
const { sourceMap = undefined, cacheDirectory = '.linaria-cache', ...rest } =
loaderUtils.getOptions(this) || {};
const options = loaderUtils.getOptions(this) || {};
validateOptions(schema, options, 'Linaria Loader');
const {
sourceMap = undefined,
cacheDirectory = '.linaria-cache',
...pluginOptions
} = options;

const filePrefix = process.env.NODE_ENV === 'production' ? 'prod' : 'dev';

const outputFilename = path.join(
path.isAbsolute(cacheDirectory)
? cacheDirectory
: path.join(this.context, cacheDirectory),
: path.join(this.rootContext, cacheDirectory),
path.relative(
this.context,
this.resourcePath.replace(/\.[^.]+$/, '.linaria.css')
this.rootContext,
this.resourcePath.replace(/\.[^.]+$/, `.${filePrefix}.linaria.css`)
)
);

Expand Down Expand Up @@ -57,7 +89,7 @@ export default function loader(
filename: this.resourcePath,
inputSourceMap: inputSourceMap != null ? inputSourceMap : undefined,
outputFilename,
pluginOptions: rest,
pluginOptions,
});
} finally {
// Restore original behaviour
Expand Down

0 comments on commit c9627c6

Please sign in to comment.