Skip to content

Commit

Permalink
Fix Svelte plugin SSR bug; add test
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Oct 7, 2020
1 parent b21848b commit d9ec0ef
Show file tree
Hide file tree
Showing 7 changed files with 299 additions and 16 deletions.
4 changes: 4 additions & 0 deletions plugins/plugin-svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@
"gitHead": "a01616bb0787d56cd782f94cecf2daa12c7594e4",
"dependencies": {
"rollup-plugin-svelte": "^5.2.3"
},
"devDependencies": {
"node-sass": "^4.14.1",
"svelte-preprocess-sass": "^0.2.0"
}
}
6 changes: 4 additions & 2 deletions plugins/plugin-svelte/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ module.exports = function plugin(snowpackConfig, pluginOptions = {}) {

let svelteOptions;
let preprocessOptions;
const userSvelteConfigLoc = path.join(process.cwd(), 'svelte.config.js');
// Note(drew): __config is for internal testing use; maybe we should make this public at some point?
const userSvelteConfigLoc =
pluginOptions.__config || path.join(process.cwd(), 'svelte.config.js');
if (fs.existsSync(userSvelteConfigLoc)) {
const userSvelteConfig = require(userSvelteConfigLoc);
const {preprocess, ..._svelteOptions} = userSvelteConfig;
Expand Down Expand Up @@ -48,7 +50,7 @@ module.exports = function plugin(snowpackConfig, pluginOptions = {}) {

const {js, css} = svelte.compile(codeToCompile, {
...svelteOptions,
generate: isSSR ? 'ssr' : 'dom',
generate: svelteOptions.generate || (isSSR ? 'ssr' : 'dom'), // let user-defined option take precedence
outputFilename: filePath,
filename: filePath,
});
Expand Down
8 changes: 8 additions & 0 deletions plugins/plugin-svelte/test/Button.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<style lang="scss">
button {
color: rgb(blue, 0.7);
font-size: 16px;
}
</style>

<button>I’m a button!</button>
3 changes: 3 additions & 0 deletions plugins/plugin-svelte/test/__snapshots__/plugin.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`@snowpack/plugin-svelte unmocked generates code 1`] = `Promise {}`;
69 changes: 69 additions & 0 deletions plugins/plugin-svelte/test/plugin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const mockCompiler = jest.fn().mockImplementation(() => ({js: ''}));
const mockPreprocessor = jest.fn();

const path = require('path');

const mockConfig = {buildOptions: {sourceMaps: false}, installOptions: {rollup: {plugins: []}}};
const mockComponent = path.join(__dirname, 'Button.svelte');

describe('@snowpack/plugin-svelte', () => {
describe('mocked ', () => {
let plugin;

beforeAll(() => {
jest.mock('svelte/compiler', () => ({compile: mockCompiler, preprocess: mockPreprocessor})); // important: mock before import
plugin = require('../plugin');
});

afterAll(() => {
jest.unmock('svelte/compiler');
});

afterEach(() => {
mockCompiler.mockClear();
mockPreprocessor.mockClear();
});

it('passes options to compiler', () => {
const options = {
generate: 'ssr',
isDev: false,
};
const sveltePlugin = plugin(mockConfig, options);
sveltePlugin.load({filePath: mockComponent});
const passedOptions = mockCompiler.mock.calls[0][1];

// this tests that all options passed above made it to the compiler
// objectContaining() allows additional options to be passed, but we only care that our options have been preserved
expect(passedOptions).toEqual(expect.objectContaining(options));
});

it('handles preprocessing', () => {
// test-only config option
const options = {__config: path.join(__dirname, 'svelte.config.js')};

const sveltePlugin = plugin(mockConfig, options);

sveltePlugin.load({filePath: mockComponent});
jest.mock('svelte/compiler', () => ({compile: jest.fn(), preprocess: mockPreprocessor}));

// as long as this function has been called, we can assume success
expect(mockPreprocessor).toHaveBeenCalled();
});
});

describe('unmocked', () => {
let plugin;

beforeAll(() => {
plugin = require('../plugin');
});

it('generates code', () => {
const options = {};
const sveltePlugin = plugin(mockConfig, options);
const result = sveltePlugin.load({filePath: mockComponent});
expect(result).toMatchSnapshot();
});
});
});
7 changes: 7 additions & 0 deletions plugins/plugin-svelte/test/svelte.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const {sass} = require('svelte-preprocess-sass');

module.exports = {
preprocess: {
style: sass({}, {name: 'css'}),
},
};
Loading

0 comments on commit d9ec0ef

Please sign in to comment.